package streams;

import java.util.stream.Stream;

public class StreamDemo {
    public static void main(String[] args) throws InterruptedException {
        // 单线程串行
        Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).reduce((a, b) -> {
            System.out.println(String.format("%s: %d + %d = %d",
                    Thread.currentThread().getName(), a, b, a + b));
            return a + b;
        }).ifPresent(System.out::println);

        Thread.sleep(2000);

        // 多线程并行
        Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).parallel().reduce((a, b) -> {
            System.out.println(String.format("%s: %d + %d = %d",
                    Thread.currentThread().getName(), a, b, a + b));
            return a + b;
        }).ifPresent(System.out::println);
    }
}
